﻿using Microsoft.Extensions.Hosting;

namespace HangfireTest.Service
{
    public class FirstStartService : IHostedService
    {
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await Task.Run(() => { Console.WriteLine("FirstStartService......StartAsync"); }, cancellationToken);

            //hangfire定时任务
            var id = Hangfire.BackgroundJob.Enqueue(() => Console.WriteLine("插入队列的任务"));
            Hangfire.BackgroundJob.Schedule(() => Console.WriteLine("延迟的任务"), TimeSpan.FromSeconds(5));
            Hangfire.RecurringJob.AddOrUpdate(() => Console.WriteLine("循环执行的任务"), Hangfire.Cron.Minutely);
            Hangfire.BackgroundJob.ContinueJobWith(id, () => Console.WriteLine("指定任务执行之后执行的任务"));
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await Task.Run(() => { Console.WriteLine("FirstStartService......StopAsync"); }, cancellationToken);
        }
    }
}
